﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;

namespace intelliEssay.Core.Platform
{
    /// <summary>
    /// The intelliEssay platform capability probing features.
    /// </summary>
    public static class FeatureProbing
    {
        /// <summary>
        /// Registered Components. Components must be registered at registeredComponent.xml in the root path for this method to retrieve.
        /// </summary>
        /// <returns></returns>
        public static ComponentCollection GetRegisteredComponents()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ComponentCollection));
            try
            {
                using (Stream stream = File.Open("registeredComponent.xml", FileMode.OpenOrCreate))
                {
                    return serializer.Deserialize(stream) as ComponentCollection;
                }
            }
            catch
            {
                if (File.Exists("registeredComponent.xml") == false)
                {
                    using (Stream stream = File.Open("registeredComponent.xml", FileMode.Create))
                    {
                        var some = new ComponentCollection();
                        some.Add(new CheckerComponent() { FilePath = "samplePath" });
                        serializer.Serialize(stream, some);
                    }
                }
                return new ComponentCollection();
            }
        }
    }

    /// <summary>
    /// The container wrapper for a list of Components
    /// </summary>
    public class ComponentCollection : List<Component>
    {
        public ComponentCollection()
        {
        }
        public ComponentCollection(IEnumerable<Component> components)
        {
            this.AddRange(components);
        }
    }
}
